<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>在线KML转geojson工具</title>
    <meta name="baidu-site-verification" content="code-c1HFZoF9HX" />
    <script type="module">
        import { kml } from "./togeojson.js";

        window.change1= function (e,compress){

            getFileContent(e, function (evt,str) {
                // console.log(
                //     evt
                // );
                console.log(str);
                var json = kml(new DOMParser().parseFromString(str, "text/xml"));
                if(compress){
                    var jsonstr = JSON.stringify(json);
                }else{
                    var jsonstr = JSON.stringify(json, null, 4);
                }
                console.log(jsonstr);
                debugger
                exportRaw('text.json', jsonstr);
            });
        }
        function fakeClick(obj) {
            var ev = document.createEvent("MouseEvents");
            ev.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            obj.dispatchEvent(ev);
        }

        function exportRaw(name, data) {
            var urlObject = window.URL || window.webkitURL || window;
            var export_blob = new Blob([data]);
            var save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
            save_link.href = urlObject.createObjectURL(export_blob);
            save_link.download = name;
            fakeClick(save_link);
        }
        var getFileContent = function (fileInput, callback) {
            if (fileInput.files && fileInput.files.length > 0 && fileInput.files[0].size > 0) {
                //下面这一句相当于JQuery的：var file =$("#upload").prop('files')[0];
                var file = fileInput.files[0];
                if (window.FileReader) {
                    var reader = new FileReader();
                    reader.onloadend = function (evt) {
                        if (evt.target.readyState == FileReader.DONE) {
                            callback(evt,evt.target.result);
                        }
                    };
                    // 包含中文内容用gbk编码
                    reader.readAsText(file, 'utf-8');
                }
            }
        };
    </script>
</head>
<body>
<div>
    <h1>在线KML转geojson工具，源码地址：<a href="https://gitee.com/ayezs/kml-to-geojson"> KML转geojson</a></h1>
    <h2>建议使用utf-8编码的文件，否则可能出现乱码</h2>
    <h2>另外我还有一个数据抽稀的项目，可以看下下面的链接: <a href="https://gitee.com/ayezs/kml-data-thinning">数据抽稀项目地址</a></h2>
    <p>
        不压缩:
        <input id="file" onchange="change1(this)" type="file"/>
    </p>
    <p>
        压缩:
        <input id="file2" onchange="change1(this,true)" type="file"/>
    </p>

</div>

</body>
</html>